package com.awspaas.user.apps.cpq.controller;

import java.util.HashMap;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOCopyAPI;
import com.actionsoft.sdk.local.api.BOQueryAPI;

public class CPQ_GetNewInstallationFeeWeb {

	public String getNewInstallationFee(UserContext userContext, String bindId, String type) {
		try {
			String pid="";
			if(type.equals("创建新模板")) {
				BOQueryAPI data=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE").bindId(bindId);
				BOQueryAPI parameterQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P").bindId(bindId);
				BOQueryAPI gridsQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_GRID").bindId(bindId);
				BOQueryAPI priceQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_PRICE").bindId(bindId);
				//创建新的模块
				 pid=SDK.getProcessAPI().createBOProcessInstance("obj_8f31cff02b35409f8fe4d96fab1ef340",userContext.getUID(), "").getId();
				BOCopyAPI newData=data.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE", pid);
				newData.addNewData("DZSM","");
				newData.addNewData("BBH","V1.0");
				newData.addNewData("VERSION","1");
				newData.addNewData("STATUS","拟制");
				newData.addNewData("CJLX",type);
				newData.exec();
				BOCopyAPI newGrids=gridsQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_GRID", pid);
				newGrids.exec();
				BOCopyAPI newParameter=parameterQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P", pid);
				newParameter.exec();
				BOCopyAPI newPrice=priceQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_PRICE", pid);
				newPrice.exec();
				//SDK.getBOAPI().query("").bindId(bindId);
				
			}else if(type.equals("新建")) {
				 pid=SDK.getProcessAPI().createBOProcessInstance("obj_8f31cff02b35409f8fe4d96fab1ef340",userContext.getUID(), "").getId();				
                BO data=new BO();
				data.set("VERSION", "1");
				data.set("BBH", "V1.0");
				data.set("CJLX", "");
				data.set("STATUS", "启用");
				SDK.getBOAPI().create("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE", data, pid, userContext.getUID());
				//获取安装费基本参数信息
				BOQueryAPI parameterQuery=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_PARAMETER_MAP").addQuery("SFYSF=", "是");
				//获取安装费费用明细信息
				BOQueryAPI priceQuery=SDK.getBOAPI().query("BO_EU_CPQ_IFT_PRICE");
				BOCopyAPI newParameter=parameterQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P", pid);
				newParameter.exec();
				BOCopyAPI newPrice=priceQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_PRICE", pid);
				newPrice.exec();
				
			}else if(type.equals("创建新版本")) {
				BOQueryAPI data=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE").bindId(bindId);
				BOQueryAPI parameterQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P").bindId(bindId);
				BOQueryAPI gridsQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_GRID").bindId(bindId);
				BOQueryAPI priceQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_PRICE").bindId(bindId);
				//创建新的模块
				 pid=SDK.getProcessAPI().createBOProcessInstance("obj_8f31cff02b35409f8fe4d96fab1ef340",userContext.getUID(), "").getId();
				BOCopyAPI newData=data.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE", pid);
				BO bo=data.detail();
				String vString=bo.getString("VERSION");
				int num=Integer.valueOf(vString);
				int newNum=num+1;
				newData.addNewData("BBH","V"+newNum+".0");
				newData.addNewData("VERSION",newNum);
				newData.addNewData("STATUS","拟制");
				newData.addNewData("CJLX",type);
				newData.exec();
				BOCopyAPI newGrids=gridsQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_GRID", pid);
				newGrids.exec();
				BOCopyAPI newParameter=parameterQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P", pid);
				newParameter.exec();
				BOCopyAPI newPrice=priceQuery.copyTo("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_PRICE", pid);
				newPrice.exec();	
			}
			ResponseObject ro = ResponseObject.newOkResponse();
			Map<String, String> map=new HashMap<String, String>();
			map.put("bindId", pid);
			ro.setData(map);
			return ro.toString();
		} catch (Exception e) {
			throw e;
		}
		
	}

}
